/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import edu.doeacc.tms.domain.Truck;
import edu.doeacc.tms.web.bean.TruckBean;

/**
 *
 * @author slok
 */
@FacesConverter(forClass = Truck.class)
public class TruckConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uic, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        TruckBean controller = (TruckBean) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "truckBean");
        return controller.getTruckFacade().find(Long.valueOf(value));
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Truck) {
            Truck o = (Truck) object;
            return String.valueOf(o.getId());
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Truck.class.getName());
        }
    }
}
